"use strict";
/* Controllers */
app.controller("healthScoreDetailController", ['$scope', '$compile', '$location', '$rootScope', '$routeParams', 'healthScoreDetailService', function ($scope, $compile, $location, $rootScope, $routeParams, healthScoreDetailService) {

    var vm = $scope.vm = {};
    vm.p = {};
    var dt = $routeParams.dt;

    if (dt == null || typeof (dt) == 'undefined') {
        dt = new Date(new Date().getTime() - 2*86400000).format('yyyyMMdd');
    }
    var tableName = $routeParams.tableName;
    vm.p.dt = dt
    vm.p.tableName = tableName

    function getNewObjectOf(src) {
        return Object.assign({}, src);
    }

    var errorFn = function (result) {
        alert(result);
    };


    $scope.getHealthScoreDetail = function () {
        var vm = $scope.vm;
        var params = vm.p
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.p = res.data
                var realHealthScore = 0
                var healthScore = parseInt(vm.p.healthScore)
                if (vm.p.configLifecycle != '' && vm.p.configLifecycle != null && (healthScore < 0 || healthScore == null)) {
                    realHealthScore = 20
                } else if (healthScore < 0 && (vm.p.configLifecycle == '' || vm.p.configLifecycle == null)) {
                    realHealthScore = 0
                } else if (healthScore > 100) {
                    realHealthScore = 100
                } else {
                    realHealthScore = healthScore
                }
                vm.p.realHealthScore = realHealthScore
                vm.p.dropScore = Math.min(vm.p.configLifecycle-vm.p.recommendLifecycle,80);
                if(vm.p.dropScore<0) {
                    vm.p.dropScore = 0;
                }
            } else {
                alert("暂无数据");
            }
        };
        return healthScoreDetailService.getHealthScoreDetail(params, procFn, errorFn);
    };

    if (typeof (tableName) != "undefined" && tableName != null) {
        $scope.getHealthScoreDetail()
    }


}]);

/* Services */
app.service('healthScoreDetailService', ['ajaxService', function (ajaxService) {

    this.getHealthScoreDetail = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/healthScore/getStorageHealthScoreDetail.do", successFunction, errorFunction);
    };

}]);